<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item :label="$t('loginLog.title')" prop="title">
        <el-input
            v-model="queryParams.title"
            :placeholder="$t('loginLog.titleInput')"
            clearable
            style="width: 240px;"
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('loginLog.operName')" prop="operName">
        <el-input
            v-model="queryParams.operName"
            :placeholder="$t('loginLog.operNameInput')"
            clearable
            style="width: 240px;"
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('program.status')" prop="status">
        <el-select
            v-model="queryParams.status"
            :placeholder="$t('loginLog.operStatusInput')"
            clearable
            size="small"
            style="width: 240px"
        >
          <el-option
              v-for="dict in statusOptions"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('loginLog.operTime')">
        <el-date-picker
            v-model="dateRange"
            size="small"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            :start-placeholder="$t('elButton.startTime')"
            :end-placeholder="$t('elButton.endTime')"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{
            $t('elButton.search')
          }}
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('elButton.reset') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            v-hasPerm="['operLog_del']"
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDel"
        >{{ $t('elButton.delete') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPerm="['operLog_del']"
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="handleClean"
        >{{ $t('elButton.clear') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPerm="['operLog_export']"
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
        >{{ $t('elButton.export') }}
        </el-button>
      </el-col>

      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" :content="$t('elButton.refresh')" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery"/>
        </el-tooltip>
        <el-tooltip class="item" effect="dark"
                    :content="showSearch ? $t('elButton.hideSearch') : $t('elButton.showSearch')" placement="top"
        >
          <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch"/>
        </el-tooltip>
      </div>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column :label="$t('loginLog.operId')" align="center" prop="id"/>
      <el-table-column :label="$t('loginLog.title')" align="center" prop="title"/>
      <el-table-column :label="$t('dictionaries.askStyle')" align="center" prop="method"/>
      <el-table-column :label="$t('loginLog.operName')" align="center" prop="operName"/>
      <el-table-column :label="$t('equipment.ip')" align="center" prop="operIp" width="130"
                       :show-overflow-tooltip="true"
      />
      <el-table-column :label="$t('loginLog.operAddr')" align="center" prop="operAddr"/>
      <el-table-column :label="$t('loginLog.clientId')" align="center" prop="clientId"/>
      <el-table-column :label="$t('loginLog.executeTime')" align="center" prop="executeTime"/>
      <el-table-column :label="$t('loginLog.operStatusInput')" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column :label="$t('loginLog.operDate')" align="center" prop="operTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.operTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('equipment.control')" align="center" class-name="small-padding fixed-width"
                       width="120" fixed="right"
      >
        <template slot-scope="scope">
          <el-button
              v-hasPerm="['operLog_view']"
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row,scope.index)"
          >{{ $t('monitor.detail') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.current"
        :limit.sync="queryParams.size"
        @pagination="getList"
    />

    <!-- 操作日志详细 -->
    <el-dialog :title="$t('loginLog.operLogDetail')" :visible.sync="open" width="700px" :modal-append-to-body="true"
               :append-to-body="true"
    >
      <el-form ref="form" :model="form" label-width="100px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('loginLog.operMode') + '：'">{{ form.title }}</el-form-item>
            <el-form-item
                :label="$t('loginLog.operLoginInfo') + '：'"
            >{{ form.operName }} / {{ form.operIp }} / {{ form.operAddr }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('loginLog.operUrl') + '：'">{{ form.operUrl }}</el-form-item>
            <el-form-item :label="$t('loginLog.operMethod') + '：'">{{ form.method }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('loginLog.clientId') + '：'">{{ form.clientId }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('loginLog.executeTime') + '：'">{{ form.executeTime }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('loginLog.userAgent') + '：'">{{ form.userAgent }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('loginLog.operParam') + '：'">{{ form.operParam }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('loginLog.operStatusInput') + '：'">
              <div v-if="form.status === 0">{{ $t('normal') }}</div>
              <div v-else-if="form.status === 1">{{ $t('fail') }}</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('loginLog.operTime') + '：'">{{ parseTime(form.operTime) }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item v-if="form.status === 1" :label="$t('loginLog.errorMsg') + '：'">
              <span v-if="form.stackMsg" style="color:#409EFF;cursor:pointer" @click="showContent()"
              >{{ formatLongText(form.stackMsg) }}</span>
              <span v-else>{{ $t('notYet') }}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">{{ $t('closeText') }}</el-button>
      </div>
    </el-dialog>

    <!-- 完整内容 -->
    <el-dialog :title="$t('loginLog.allContent')" :visible.sync="dialogContent" :modal-append-to-body="true"
               :append-to-body="true"
    >
      <el-form>
        <el-form-item>
          <el-input v-model="form.stackMsg" type="textarea" :autosize="{ minRows: 4, maxRows: 18}"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogContent = false">{{ $t('closeText') }}</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { list, delOperLog, cleanOperLog, exportOperLog } from '@/api/monitor/operLog'

export default {
  name: 'OperLog',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 是否显示弹出层
      open: false,
      // 类型数据字典
      statusOptions: [],
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        title: undefined,
        operName: undefined,
        status: undefined
      },
      // 显示搜索条件
      showSearch: true,
      // 完整内容标志位
      dialogContent: false
    }
  },
  created() {
    this.getList()
    this.getDicts('success_fail').then(response => {
      this.statusOptions = response.data
    })
  },
  methods: {
    /** 查询登录日志 */
    getList() {
      this.loading = true
      list(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.list = response.data
        this.total = response.total
        this.loading = false
      })
    },
    // 操作日志状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.multiple = !selection.length
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.open = true
      this.form = row
    },
    /** 删除按钮操作 */
    handleDel(row) {
      const ids = row.id || this.ids
      this.$confirm(this.$t('loginLog.deleteOper') + ids + this.$t('equipment.deleteEquipment2'), this.$t('warning'), {
        confirmButtonText: this.$t('submitText'),
        cancelButtonText: this.$t('cancelText'),
        type: 'warning'
      }).then(function() {
        return delOperLog(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('deleteSuccess'))
      }).catch(function() {
      })
    },
    /** 清空按钮操作 */
    handleClean() {
      this.$confirm(this.$t('loginLog.emptyOper'), this.$t('warning'), {
        confirmButtonText: this.$t('submitText'),
        cancelButtonText: this.$t('cancelText'),
        type: 'warning'
      }).then(function() {
        return cleanOperLog()
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('loginLog.emptySuccess'))
      }).catch(function() {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      if (this.ids.length === 0) {
        this.msgWarning(this.$t('equipment.equipmentInput'))
        return
      }
      const queryParams = this.queryParams
      this.$confirm(this.$t('loginLog.exportOper'), this.$t('warning'), {
        confirmButtonText: this.$t('submitText'),
        cancelButtonText: this.$t('cancelText'),
        type: 'warning'
      }).then(() => {
        return exportOperLog(queryParams,this.ids)
      }).then(response => {
        this.download(response.data)
      }).catch(function() {
      })
    },
    formatLongText(value) {
      if (value) {
        if (value.length > 10) {
          return value.substr(0, 70) + '...'
        } else {
          return value
        }
      } else {
        return this.$t('notYet')
      }
    },
    showContent() {
      this.dialogContent = true
    }
  }
}
</script>

